package view{
	import data.Tower;
	
	import events.GameEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.utils.Timer;
	
	import mapData.TowerData;
	
	import utils.SwfElements;
	
	public class TowerView extends Sprite{
		
		/**
		 * 声明存放各个元件的SWF文件 
		 */		
		private var swfElement:SwfElements;
		
		/**
		 * 塔的最终显示元件 
		 */		
		private var tView:MovieClip;
		
		/**
		 * 所要建设的塔的数据对象 
		 */		
		private var tower:Tower;
		/**
		 * 塔在建造过种中的进度条显示 对象
		 */		
		private var rectBar:RectBar;
		
		/**
		 * 建造过程的显示对象 
		 */		
		private var buildingView:MovieClip;
		
		/**
		 * 时间对象用于控制建造过程持续时间 
		 */		
		private var timer:Timer;

		private var arrow:Arrow
		public function TowerView(_tower:Tower,_arrow:Arrow){
			tower = _tower;
			swfElement = new SwfElements();
			addChild(swfElement);
			arrow = _arrow
			//监听SWF文件是否加载完成
			addEventListener(GameEvent.SWF_LOADER_COMPLETE,swfCompleteHandle);
		}
		
		/**
		 * SWF加载完成后添加相应对显示对象和控制 
		 * @param event
		 * 
		 */		
		private function swfCompleteHandle(event:GameEvent):void{
			addTower();
			buildingView = swfElement.getMC(TowerData.BUILDINGMC);
			addChild(buildingView);
			rectBar = new RectBar(57,10);
			addChild(rectBar);
			
			buildingView.x = tView.x;
			buildingView.y = tView.y;
			
			rectBar.x = tView.x-tView.width/2;
			rectBar.y = tView.y-tView.height/2;
			
			timer = new Timer(20,50);
			timer.addEventListener(TimerEvent.TIMER,timerHandle);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerCompleteHandle);
			timer.start();
		}
		
		/**
		 * 建造过程完成,益处相应对象
		 * @param event
		 * 
		 */		
		private function timerCompleteHandle(event:TimerEvent):void{
			removeChild(buildingView);
			addChild(tView);
			addChild(arrow);
			removeChild(rectBar);
			rectBar=null;
			timer.removeEventListener(TimerEvent.TIMER,timerHandle);
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE,timerCompleteHandle);
		}
		
		/**
		 * 进度条显示 控制 
		 * @param event
		 * 
		 */		
		protected function timerHandle(event:TimerEvent):void{
			var w:Number = timer.currentCount * 1.11;
			if(w<=55){
				rectBar.reWidth = w;
			}else {
				rectBar.reWidth = 55;
			}
		}
		
		/**
		 * 根据传入的塔的数据,从SWF中读取相应塔的显示对象 
		 * 
		 */		
		private function addTower():void{
			var towerName:String = tower.type.symbol;
			tView = swfElement.getMC(towerName);
			
			tView.x = tower.px;
			tView.y = tower.py - 10;
			
			tView.buttonMode = true;
			
			tView.addEventListener(MouseEvent.MOUSE_OVER,onTViewMouseOver);
			tView.addEventListener(MouseEvent.MOUSE_OUT,onTViewMouseOut);
			tView.addEventListener(MouseEvent.CLICK,onTViewClick);
		}
		
		/**
		 * 鼠标移动到塔上时  添加滤镜
		 * @param event
		 * 
		 */		
		private function onTViewMouseOver(event:MouseEvent):void{
			var mc:MovieClip = event.target as MovieClip;
			mc.filters = [ new GlowFilter( 0xFBF803,1,15,15) ];
			
		}
		
		/**
		 * 鼠标移开塔时   去除滤镜
		 * @param event
		 * 
		 */	
		private function onTViewMouseOut(event:MouseEvent):void{
			var mc:MovieClip = event.target as MovieClip;
			mc.filters = null;
			
		}
		private function onTViewClick(event:MouseEvent):void{
			
			
			var mc:MovieClip = event.target as MovieClip;
			var evt:GameEvent = new GameEvent(GameEvent.UPGRADEBAR_SHOW,mc);
			
			this.dispatchEvent(evt);
			trace(evt);
			
			/*var upgradeBar:UpgradeBar = UpgradeBar.getInstence();
			upgradeBar.x = mc.x;
			upgradeBar.y = mc.y;
			addChild(upgradeBar);
			upgradeBar.orShow();*/
			
		}
		
	}
}